<?php
class AlbumPicsController extends AlbumAppController {
	public $components = array ('Acl','Auth' );
	public $uses = array ('Album.AlbumPic' );

	/**
	 * 相册图片列表
	 * @author lj@gootop.net  
	 * @Date 2012-11-16
	 * @param
	 * @return 
	 */
	function admin_index($id) {
		$this->paginate = array(
				'AlbumPic' => array(
						'conditions' => array('album_id'=>$id),
						'order' => 'AlbumPic.id DESC',
				)
		);
		$albumPicList = $this->paginate();
		$this->set ( 'albumPicList', $albumPicList );
		$this->set ( 'album_id', $id );
	}
	/**
	 * 添加图片
	 * @author lj@gootop.net  
	 * @Date 2012-11-16
	 * @param
	 * @return 
	 */
	function add($id) {
		$this->set ( 'album_id', $id );
		if (! empty ( $this->data )) {
			$album = $this->request->data;
			$album['AlbumPic']['album_id'] = $id;
			$album['AlbumPic']['user_id'] = $this->Auth->user('id');
			$rs = $this->AlbumPic->addNewPic($album);
			if(!$rs['state']){
				$this->Session->setFlash ( $rs['msg'] );
			}else{
				$this->Session->setFlash ( '添加成功！' );
			}
			$this->redirect ( array ('action' => 'index',$id) );
		} else {
			$this->render ( 'add' );
		}
	}
	
	/**
	 * 异步上传
	 * @author zmx@gootop.net  
	 * @Date 2012-11-19
	 * @param
	 * @return 
	 */
	function ajaxPicUpload($album_id) {
		$this->layout = 'ajax';
		$album = $this->request->data;
		$album['AlbumPic']['album_id'] = $album_id;
		$album['AlbumPic']['user_id'] = $this->Auth->user('id');
		$rs = $this->AlbumPic->addNewPic($album);
		$this->set('state',$rs['state']);
		$this->set('msg',$rs['msg']);
		$this->set('data',$rs['data']);
	}
	
	/**
	 * 头像上传
	 * @author zmx@gootop.net  
	 * @Date 2012-12-6
	 * @param
	 * @return 
	 */
	function ajaxheader(){
		$this->layout = 'ajax';
		$album = $this->request->data;
		$album['AlbumPic']['album_id'] = 6;
		$album['AlbumPic']['user_id'] = $this->Auth->user('id');
		$rs = $this->AlbumPic->addNewPic($album);
		$this->loadModel('User');
		$this->User->id = $this->Auth->user('id');
		$this->User->set('sys_photo_id',$rs['data']['AlbumPic']['id']);
		$this->User->save();
		$this->set('state',$rs['state']);
		$this->set('msg',$rs['msg']);
		$this->set('data',$rs['data']);
		$this->render('ajax_pic_upload');
	}
	
/**
	 * 档案上传
	 * @author zmx@gootop.net  
	 * @Date 2012-12-6
	 * @param
	 * @return 
	 */
	function ajaxArchive(){
		$this->layout = 'ajax';
		$album = $this->request->data;
		$album['AlbumPic']['album_id'] = 3;
		$album['AlbumPic']['user_id'] = $this->Auth->user('id');
		$rs = $this->AlbumPic->addNewPic($album);
		$this->set('state',$rs['state']);
		$this->set('msg',$rs['msg']);
		$this->set('data',$rs['data']);
		$this->render('ajax_pic_upload');
	}
	
	/**
	 * 头像剪裁
	 * @author zmx@gootop.net  
	 * @Date 2012-12-6
	 * @param
	 * @return 
	 */
	function ajaxheadercut($photo_id=null){
		$AlbumPic = $this->AlbumPic->findById($photo_id);
		$crop = $this->request->data['Crop']; 
		$rs = $this->AlbumPic->photocut($AlbumPic['AlbumPic']['file_path'],
			$crop['x'],$crop['y'],$crop['w'],$crop['h'],
			$crop['zoom'],$AlbumPic['AlbumPic']['file_type'],array('x'=>150,'y'=>150)
		);
		if($rs['state']){
			$this->loadModel('User');
			$avatarUser = array(
				'User'=>array(
					'id'=>$this->Auth->user('id'),
					'avatar'=>$rs['msg']
				)
			);
			if ($this->User->setAvatar($avatarUser)) {
				$this->Session->write('Auth.User.avatar',$rs['msg']) ;
				$rs['state']= true;
			}else{
				$rs['state']= false;
			};
		}
		$this->set('state',$rs['state']);
		$this->set('msg',$rs['msg']);
		$this->render('ajax_pic_upload');
	}
	
	/**
	 * 删除图片
	 * @author lj@gootop.net  
	 * @Date 2012-11-16
	 * @param
	 * @return
	 */
	function delete($album_id, $id) {
		$result = $this->AlbumPic->deleteNewsPic ($this->Auth->user('id'),$id);
		$this->Session->setFlash ( $result['msg'] );
		$this->redirect ( array ('action' => 'index',$album_id) );
	}
	/**
	 * 获取相册下的图片
	 * @author lj@gootop.net  
	 * @Date 2012-11-21
	 * @param
	 * @return 
	 */
	function ajaxPicList($id,$id_pre,$page){
		$this->layout='ajax';
		$this->paginate = array(
				'AlbumPic' => array(
						'conditions' => array(
								'AlbumPic.album_id' => $id,//相册id
								'AlbumPic.user_id' => $this->Auth->user('id')),
						'order' => 'AlbumPic.id DESC',
						'limit'=>8,
						'page' => !empty($page)?$page:1,
						));
		$albumPicResult = $this->paginate();
		
		$this->set('page',$this->params['paging']['AlbumPic']['page']);//当前页
		$this->set('count',$this->params['paging']['AlbumPic']['count']);//总记录数
		$this->set('totalPage',$this->params['paging']['AlbumPic']['pageCount']);//总页数
		
		$this->set('albumPicResult',$albumPicResult);
		$this->set('album_id',$id);
		$this->set('id_pre',$id_pre);
		$this->set('state',true);
	}
	/**
	 * 查看图片信息
	 * @author lj@gootop.net  
	 * @Date 2012-11-27
	 * @param
	 * @return 
	 */
	function view($album_id, $id) {
		$this->AlbumPic->id = $id;
		if (! $this->AlbumPic->exists ()) {
			throw new NotFoundException ( __ ( 'Invalid user' ) );
		}
		if(!empty($album_id)){
			$this->loadModel('Album.Album');
			$AlbumList = $this->Album->getAlbumById($album_id);
			$this->set ('album_title',$AlbumList['Album']['title']);
		}
		$this->set ( 'AlbumPic', $this->AlbumPic->read ( null, $id ) );
		$this->set ('album_id',$album_id);
	}
}
